const path = require('path');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const webpack = require('webpack');
const env = process.env.NODE_ENV;
const cssloader = [env === 'production' ? MiniCssExtractPlugin.loader : 'style-loader'] 

module.exports = {
    entry: {
        main: './src/main.js'
    },
    output: {
        filename: 'bundle.[hash:8].js',
        path: path.join(__dirname, 'dist')
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                use: {
                    loader: 'babel-loader',
                },
                exclude: '/node_modules/'
            },
            {
                test: /\.vue$/,
                use: ['vue-loader']
            },
            {
                test: /\.css$/,
                use: [...cssloader, 'css-loader', 'postcss-loader']
            },
            {
                test: /\.less$/,
                use: ['style-loader', 'css-loader', 'postcss-loader', 'less-loader']
            },
            {
                test: /\.(jpe?g|png|gif)$/,
                use: {
                    loader: 'url-loader',
                    options: {
                        esModule: false, // 不加的话会有这种情况 img属性src="[object Module]"
                        limit: 0, // 10kb以下的文件转换成dataUrl
                        publicPath: 'images/', 
                        outputPath: 'images', // 大于limit将打包到这里
                        name: '[name].[hash:8].[ext]'
                    }
                }
            }
        ]
    },
    plugins: [
        new VueLoaderPlugin(),
        new HtmlWebpackPlugin({
            title: 'webpack',
            template: './public/index.html',
            chunks:['main']
        }),
        new webpack.DefinePlugin({
            BASE_URL: JSON.stringify('./public/')
        })
    ],
    optimization: {
        usedExports: true,
        minimize: true,
        splitChunks: {
            chunks: 'all'
        }
    }
};